package com.liang.interview.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liang.auth.api.entity.Result;
import com.liang.interview.api.common.PageDTO;
import com.liang.interview.api.req.InterviewHistoryReq;
import com.liang.interview.api.req.InterviewReq;
import com.liang.interview.api.req.InterviewSubmitReq;
import com.liang.interview.api.req.StartReq;
import com.liang.interview.api.vo.*;
import com.liang.interview.server.entity.po.InterviewQuestionHistory;
import com.liang.interview.server.service.InterviewHistoryService;
import com.liang.interview.server.service.InterviewQuestionHistoryService;
import com.liang.interview.server.service.InterviewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/interview")
@RequiredArgsConstructor
public class InterviewController {

    private final InterviewService interviewService;
    private final InterviewHistoryService interviewHistoryService;
    private final InterviewQuestionHistoryService interviewQuestionHistoryService;

    /**
     * 分析简历
     */
    @PostMapping(value = "/analyse")
    public Result<InterviewVO> analyse(@RequestBody InterviewReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("分析简历入参{}", JSON.toJSON(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getEngine()), "引擎不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getUrl()), "简历不能为空！");
            return Result.ok(interviewService.analyse(req));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分析简历异常！错误原因{}", e.getMessage(), e);
            return Result.fail("分析简历异常！");
        }
    }

    /**
     * 开始面试
     */
    @PostMapping(value = "/start")
    public Result<InterviewQuestionVO> start(@RequestBody StartReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("开始面试入参{}", JSON.toJSON(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getEngine()), "引擎不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getQuestionList()), "关键字不能为空！");
            return Result.ok(interviewService.start(req));

        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("开始面试异常！错误原因{}", e.getMessage(), e);
            return Result.fail("开始面试异常！");
        }
    }

    /**
     * 提交面试题的答案
     */
    @PostMapping("/submit")
    public Result<InterviewResultVO> submit(@RequestBody InterviewSubmitReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("提交面试题答案入参{}", JSON.toJSON(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            InterviewResultVO resultVO = interviewService.submit(req);
            interviewHistoryService.logInterview(req, resultVO);
            return Result.ok(interviewService.submit(req));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分析简历异常！错误原因{}", e.getMessage(), e);
            return Result.fail("分析简历异常！");
        }
    }

    /**
     * 分页查询面试记录
     */
    @PostMapping("/getHistory")
    public Result<PageDTO<InterviewHistoryVO>> getHistory(@RequestBody InterviewHistoryReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("分页查询面试记录入参{}", JSON.toJSON(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            return Result.ok(interviewHistoryService.getHistory(req));
        } catch (Exception e) {
            log.error("分页查询面试记录异常！错误原因{}", e.getMessage(), e);
            return Result.fail("分页查询面试记录异常！");
        }
    }

    /**
     * 查询详情
     */
    @GetMapping("/detail")
    public Result<List<InterviewQuestionHistoryVO>> getDetail(@RequestParam("id") Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("查询面试记录详情入参{}", id);
            }
            Preconditions.checkArgument(!Objects.isNull(id), "id不能为空！");
            return Result.ok(interviewQuestionHistoryService.getDetail(id));
        } catch (Exception e) {
            log.error("查询面试记录详情异常！错误原因{}", e.getMessage(), e);
            return Result.fail("查询面试记录详情异常！");
        }
    }
}
